import { _decorator, Component, Node, Slider, SliderComponent, Toggle, ToggleComponent, director } from 'cc';
import { SoundMgr } from '../../../Framework/Scripts/Managers/SoundMgr';
import { UIComponent, UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';

import { GameApp } from '../GameApp';
const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('set_UICtrl')
export class set_UICtrl extends UIComponent {
    private SliderEffec: SliderComponent = null
    private SliderMusic: SliderComponent = null
    private EffecMute:ToggleComponent=null
    private MusicMute:ToggleComponent=null
    onLoad() {


        this.SliderEffec = this.ViewNode ("SliderEffec").getComponent(SliderComponent)
        this.SliderMusic = this.ViewNode("SliderMusic").getComponent(SliderComponent)
        this.EffecMute=this.ViewNode("SliderEffec/Toggle").getComponent(ToggleComponent)
        this.MusicMute=this.ViewNode("SliderMusic/Toggle").getComponent(ToggleComponent)




        let local_soundVolume = localStorage.getItem("linghuan_SoundsVolume");
        if (local_soundVolume) {
            this.SliderEffec.progress=Number(local_soundVolume)  
            if(  this.SliderEffec.progress<=0){
                this.EffecMute.isChecked=true
            }
        }

        let local_musicVolume = localStorage.getItem("linghuan_MusicVolume");
        if (local_musicVolume) {
            this.SliderMusic.progress=Number(local_musicVolume)  
            if( this.SliderMusic.progress<=0){
                  this.MusicMute.isChecked=true
            }
        }

        this.SliderMusic.node.on('slide', this.SliderMusicCB, this);
        this.SliderEffec.node.on('slide', this.SliderEffecCB, this);

        this.EffecMute.node.on('toggle', this.EffecMuteCB, this);
        this.MusicMute.node.on('toggle', this.MusicMuteCB, this);



     //   this.view["close_btn"].setPosition(GameApp.Instance.backposBtn)



        this.AddButtonListener("close_btn", this, () => {
           
            GameApp.Instance.isBack = false
            UIMgr.Instance.DestroyUIView(this.node.name)
        })
    }
    start() {

    }
    EffecMuteCB(toggle: ToggleComponent){
        switch (toggle.isChecked) {
            case false:
                SoundMgr.Instance.isSoundMute=false
                break;

                case true:
                    SoundMgr.Instance.isSoundMute=true
                    break;
        
            default:
                break;
        }

        let local_soundVolume = localStorage.getItem("linghuan_SoundsVolume");
        if (local_soundVolume) {
            this.SliderEffec.progress=Number(local_soundVolume)  

        }

  
              

    }
    MusicMuteCB(toggle: ToggleComponent){
        switch (toggle.isChecked) {
            case false:
                SoundMgr.Instance.isMusicMute=false
                break;

                case true:
                    SoundMgr.Instance.isMusicMute=true
                    break;
        
            default:
                break;
        }

        let local_musicVolume = localStorage.getItem("linghuan_MusicVolume");
        if (local_musicVolume) {
            this.SliderMusic.progress=Number(local_musicVolume)  

        }
    }
    SliderEffecCB(slider: Slider) {
        console.log("特效音量："+slider.progress);
        SoundMgr.Instance.setSoundsVolume(slider.progress)
        if(slider.progress<=0){
            SoundMgr.Instance.isSoundMute=true
            this.EffecMute.isChecked=true
        }else{
            SoundMgr.Instance.isSoundMute=false
            this.EffecMute.isChecked=false
        }
        // 回调的参数是 slider 组件，注意这种方式注册的事件，无法传递 customEventData
    }

    SliderMusicCB(slider: Slider) {
        console.log("背景音量："+slider.progress);
        SoundMgr.Instance.setMusicVolume(slider.progress)
        if(slider.progress<=0){
            SoundMgr.Instance.isMusicMute=true
            this.MusicMute.isChecked=true
        }else{
            SoundMgr.Instance.isMusicMute=false
            this.MusicMute.isChecked=false
        }
        // 回调的参数是 slider 组件，注意这种方式注册的事件，无法传递 customEventData
    }
    update(dt: number) {

    }
}


